package com.hitown.service.impl.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hitown.dao.user.UserDao;
import com.hitown.model.Result;
import com.hitown.model.ResultGenerator;
import com.hitown.model.user.Group;
import com.hitown.model.user.UserGroup;
import com.hitown.model.user.UserInfo;
import com.hitown.service.user.UserService;
import com.hitown.util.StringUtils;
import com.hitown.util.UUIDUtils;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public Map<String, Object> getUserList(Map<String, Object> map) {
		List<UserInfo> list = userDao.getUserList(map);
    	int count = userDao.getCountUser(map);
    	map.clear();
    	map.put("code", 0);
    	map.put("data", list);
    	map.put("msg", "成功");
    	map.put("count", count);
		return map;
	}

	@Override
	public Result addUser(UserInfo userInfo) {
		userDao.addUser(userInfo);
		return ResultGenerator.genSuccessResult();
	}

	@Override
	public Result updateUser(UserInfo userInfo) {
		userDao.updateUser(userInfo);
		return ResultGenerator.genSuccessResult();
	}

	@Override
	public Result addGroup(Group group) {
		userDao.addGroup(group);
		return ResultGenerator.genSuccessResult();
	}

	@Override
	public Result addUserGroup(Map<String, Object> map) {
		userDao.addUserGroup(map);
		return ResultGenerator.genSuccessResult();
	}

	@Override
	public Map<String, Object> getListGroup(Map<String, Object> map) {
		List<Group> list = userDao.getListGroup(map);
    	int count = userDao.getCountGroup(map);
    	map.clear();
    	map.put("code", 0);
    	map.put("data", list);
    	map.put("msg", "成功");
    	map.put("count", count);
		return map;
	}
	
	@Transactional
	@Override
	public Result updateGroup(Map<String, Object> map) {
		userDao.updateGroup(map);
		String type = String.valueOf(map.get("type"));
		if("1".equals(type)){
			userDao.updateUserGroup(map);
		}
		return ResultGenerator.genSuccessResult();
	}

	@Override
	public Map<String, Object> getListUserGroup(Map<String, Object> map) {
		List<Map<String, Object>> list = userDao.getListUserGroup(map);
    	int count = userDao.getCountUserGroup(map);
    	map.clear();
    	map.put("code", 0);
    	map.put("data", list);
    	map.put("msg", "成功");
    	map.put("count", count);
		return map;
	}

	@Override
	public Result updateUserGroup(Map<String, Object> map) {
		userDao.updateUserGroup(map);
		return ResultGenerator.genSuccessResult();
	}

}
